import { merge } from 'lodash-es';
import type { Props, ItemSchema, SelectColumns } from '@/typing/helper';
import { Divider, Field, Switch } from 'vant';
import { defineComponent, h, ref, unref } from 'vue';
import { DictField, SelectField } from '@/components';

/**
 * 文本字段
 * @param field
 * @param label
 * @param props
 */
export function textField(
  field: string,
  label: string,
  props?: Props
): ItemSchema {
  return {
    component: Field,
    props: merge(
      {
        field,
        label,
        placeholder: `请输入${label}`,
        rules: props?.required
          ? [{ required: true, message: `${label}不能为空` }]
          : [],
      },
      props
    ),
  };
}

/**
 * 枚举字段
 * @param field
 * @param label
 * @param dictType
 * @param props
 */
export function dictField(
  field: string,
  label: string,
  dictType: string,
  props?: Props
): ItemSchema {
  return {
    //  todo
    component: DictField,
    props: merge(
      {
        field,
        label,
        dictType,
        placeholder: `请选择${label}`,
        rules: props?.required
          ? [{ required: true, message: `${label}不能为空` }]
          : [],
      },
      props
    ),
  };
}

/**
 * 实体字段
 * @param field
 * @param label
 * @param props
 * @param component
 */
export function entityField(
  field: string,
  label: string,
  component: any,
  props?: Props
): ItemSchema {
  return {
    props: merge(
      {
        field,
        label,
        placeholder: `请选择${label}`,
        rules: props?.required
          ? [{ required: true, message: `${label}不能为空` }]
          : [],
      },
      props
    ),
    component,
  };
}

/**
 * select字段
 * @param field
 * @param label
 * @param columns
 * @param props
 */
export function selectField(
  field: string,
  label: string,
  columns: SelectColumns,
  props?: Props
): ItemSchema {
  return {
    props: merge(
      {
        field,
        label,
        columns,
        placeholder: `请选择${label}`,
        rules: props?.required
          ? [{ required: true, message: `${label}不能为空` }]
          : [],
      },
      props
    ),
    //  todo
    component: SelectField,
  };
}

/**
 * 是否字段
 * @param field
 * @param label
 * @param props
 */
export function switchField(
  field: string,
  label: string,
  props?: Props
): ItemSchema {
  return {
    component: defineComponent(
      ({ modelValue }, { emit }) => {
        const checked = ref(modelValue);
        return () =>
          h(Field, null, {
            input: () =>
              h(Switch, {
                size: '18px',
                modelValue: checked.value,
                onChange: (value: boolean) => {
                  checked.value = value;
                  emit('update:modelValue', value);
                  emit('change', value);
                },
              }),
          });
      },
      {
        props: ['modelValue'],
        emits: ['update:modelValue', 'change'],
      }
    ),
    props: merge(
      {
        field,
        label,
      },
      props
    ),
  };
}

/**
 * 分割线
 * @param text
 * @param props
 */
export function dividerField(text: string): ItemSchema {
  return {
    component: defineComponent(() => {
      return () =>
        h(Divider, null, {
          default: () => text,
        });
    }),
  };
}
